package com.zhu.day.util;

import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.TimeUnit;

public class DateUtil {


    /**
     * @Title: getStartOrEndDayOfQuarter
     * @Description TODO 获取本季度的第一天或最后一天
     * @Param: [today, isFirst: true 表示开始时间，false表示结束时间]
     * @return: java.lang.String
     * @Exception:
     */
    public static LocalDate getStartOrEndDayOfQuarter(LocalDate today, Boolean isFirst){
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        Month month = today.getMonth();
        Month firstMonthOfQuarter = month.firstMonthOfQuarter();
        Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
        if (isFirst) {
            resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
        } else {
            resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
        }
        return resDate;
    }


    public static void main(String[] args) {
        LocalDate today = LocalDate.now();

        LocalDate startOrEndDayOfQuarter = getStartOrEndDayOfQuarter(today, true);
        LocalDate startOrEndDayOfQuarter1 = getStartOrEndDayOfQuarter(today, false);
        System.out.println(startOrEndDayOfQuarter);
        System.out.println(startOrEndDayOfQuarter1);
        long days = Math.abs(startOrEndDayOfQuarter1.until(today, ChronoUnit.DAYS));
        long seconds = TimeUnit.DAYS.toSeconds(days);
        System.out.println(days);
        System.out.println(seconds);
    }
}
